package com.yunxi.poc.starbucks.controller;


import com.yunxi.poc.starbucks.common.BaseController;
import com.yunxi.poc.starbucks.entity.Tb1Store;
import com.yunxi.poc.starbucks.service.ITb1StoreService;
import com.yunxi.poc.starbucks.util.ParameterUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author shiyuan.xlc
 * @since 2021-08-09
 */
@RestController
@RequestMapping("/starbucks/store")
public class Tb1StoreController extends BaseController {

    @Resource
    private ITb1StoreService tb1StoreService;

    @ResponseBody
    @RequestMapping(value = {"/list"}, method = RequestMethod.GET)
    public ResponseEntity listStores(HttpServletRequest request) {
        Map<String, Object> cond = ParameterUtil.wrapObjectMap(request.getParameterMap());
        List<Tb1Store> stores = tb1StoreService.listByMap(cond);
        return ResponseEntity.status(HttpStatus.OK).body(stores);
    }

    /**
     * 提供 Select 标签的数据
     *
     * @return
     */
    @GetMapping(value = "select")
    public ResponseEntity<List<Map<String, String>>> select() {
        Map<String, Object> query = new HashMap<>(1);
        query.put("valid_flag", 1);
        List<Tb1Store> stores = tb1StoreService.listByMap(query);
        List<Map<String, String>> resultList = stores.stream().map(storeEntity -> {
            Map<String, String> map = new HashMap<>(2);
            map.put("value", storeEntity.getGlobalStoreNumber());
            map.put("name", storeEntity.getStoreName());
            return map;
        }).collect(Collectors.toList());
        return ResponseEntity.status(HttpStatus.OK).body(resultList);
    }
}
